package com.anxin.act.process.vo;

import com.anxin.act.utils.ActUtils;
import com.anxin.common.utils.DateUtils;
import com.anxin.common.utils.StringUtils;
import com.anxin.common.utils.sys.UserUtils;
import com.anxin.framework.web.entity.BaseEntity;
import com.anxin.sys.system.entity.SysUser;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.repository.ProcessDefinition;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author: liuxiangyu
 * @date: 2020/3/9 15:23
 */
public class ProcessVo extends BaseEntity {
    //流程ID
    private String id;

    //流程名称
    private String name;

    //流程Key
    private String key;

    //流程分类
    private String category;

    //流程版本
    private Integer version;

    //部署时间
    private Date deployTime;

    // 流程部署ID
    private String deploymentId;

    /**
     * 待办标题
     */
    private String title;

    /**
     * 流程状态
     */
    private String actStatus;

    /**
     * 申请时间
     */
    private String beginTime;

    /**
     * 完成时间
     */
    private String endTime;

    /**
     * 申请人ID
     */
    private String applyUserId;

    /**
     * 申请人登录名
     */
    private String applyUserName;

    /**
     * 申请人昵称
     */
    private String applyNickName;

    public ProcessVo(){

    }

    public ProcessVo(HistoricProcessInstance historicProcessInstance){

        this.id = historicProcessInstance.getId();
        if (historicProcessInstance.getStartTime() != null){
            this.beginTime = DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss",historicProcessInstance.getStartTime());
        }
        if (historicProcessInstance.getEndTime() != null){
            this.endTime = DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss",historicProcessInstance.getEndTime());
        }
        this.actStatus = this.getProcessStatus(historicProcessInstance);
        this.key = historicProcessInstance.getProcessDefinitionKey();
        this.title = historicProcessInstance.getName();

        SysUser applyUser = UserUtils.getUserByUserName(historicProcessInstance.getStartUserId());
        if (applyUser != null){
            this.applyNickName = applyUser.getNickName();
        }




    }

    /**
     * 获取流程审批状态 D:待审批  Z:审批终止  W:审批完成
     * @param historicProcessInstance
     * @return
     */
    public String getProcessStatus(HistoricProcessInstance historicProcessInstance){
        if (historicProcessInstance.getEndTime() == null){
            return "D";
        }else if(StringUtils.isNotBlank(historicProcessInstance.getDeleteReason())){
            return "Z";
        }else if(historicProcessInstance.getEndTime() != null){
            return "W";
        }

        return "";
    }

    public String getDeploymentId() {
        return deploymentId;
    }

    public void setDeploymentId(String deploymentId) {
        this.deploymentId = deploymentId;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getActStatus() {
        return actStatus;
    }

    public void setActStatus(String actStatus) {
        this.actStatus = actStatus;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public Integer getVersion() {
        return version;
    }

    public void setVersion(Integer version) {
        this.version = version;
    }

    public Date getDeployTime() {
        return deployTime;
    }

    public void setDeployTime(Date deployTime) {
        this.deployTime = deployTime;
    }

    @Override
    public String getBeginTime() {
        return beginTime;
    }

    @Override
    public void setBeginTime(String beginTime) {
        this.beginTime = beginTime;
    }

    @Override
    public String getEndTime() {
        return endTime;
    }

    @Override
    public void setEndTime(String endTime) {
        this.endTime = endTime;
    }

    public String getApplyUserId() {
        return applyUserId;
    }

    public void setApplyUserId(String applyUserId) {
        this.applyUserId = applyUserId;
    }

    public String getApplyUserName() {
        return applyUserName;
    }

    public void setApplyUserName(String applyUserName) {
        this.applyUserName = applyUserName;
    }

    public String getApplyNickName() {
        return applyNickName;
    }

    public void setApplyNickName(String applyNickName) {
        this.applyNickName = applyNickName;
    }
}
